package me.firstapp.service.weixin.product.impl;

import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.product.Product;
import me.firstapp.module.weixin.product.RebateGrid;
import me.firstapp.repository.weixin.product.RebateGridRepository;
import me.firstapp.service.weixin.product.RebateGridService;

@Service
public class RebateGridServiceImpl implements RebateGridService {

	@Autowired
	private RebateGridRepository rebateGridRepository;

	@Override
	public void executeAddUpdate(Product product) {
		List<RebateGrid> rebateGrids = rebateGridRepository.findByProductId(product.getId());
		for (RebateGrid rebateGrid : rebateGrids) {
			rebateGridRepository.deleteById(rebateGrid.getId());
		}
		RebateGrid rebateGrid1 = new RebateGrid();
		rebateGrid1.setProductId(product.getId());
		rebateGrid1.setCol1("名    称");
		rebateGrid1.setCol2(product.getName());
		rebateGrid1.setSort(1);
		rebateGridRepository.save(rebateGrid1);

		RebateGrid rebateGrid2 = new RebateGrid();
		rebateGrid2.setProductId(product.getId());
		rebateGrid2.setCol1("单   位");
		rebateGrid2.setCol2(product.getUnit());
		rebateGrid2.setSort(2);
		rebateGridRepository.save(rebateGrid2);

		RebateGrid rebateGrid3 = new RebateGrid();
		rebateGrid3.setProductId(product.getId());
		rebateGrid3.setCol1("价   格");
		rebateGrid3.setCol2("粉丝" + product.getFansPrice().setScale(2, RoundingMode.HALF_UP) + "贝币 " + "   会员"
				+ product.getBaseVipPrice().setScale(2, RoundingMode.HALF_UP) + "贝币" + "   代理"
				+ product.getHighVipPrice().setScale(2, RoundingMode.HALF_UP) + "贝币");
		rebateGrid3.setSort(3);
		rebateGridRepository.save(rebateGrid3);

		RebateGrid rebateGrid4 = new RebateGrid();
		rebateGrid4.setProductId(product.getId());
		rebateGrid4.setCol1("赠送积分");
		rebateGrid4.setCol2("粉丝" + product.getFansPrice().setScale(2, RoundingMode.HALF_UP) + "积分" + "   会员"
				+ product.getBaseVipPrice().setScale(2, RoundingMode.HALF_UP) + "积分" + "   代理"
				+ product.getHighVipPrice().setScale(2, RoundingMode.HALF_UP) + "积分");
		rebateGrid4.setSort(4);
		rebateGridRepository.save(rebateGrid4);

		RebateGrid rebateGrid5 = new RebateGrid();
		rebateGrid5.setProductId(product.getId());
		rebateGrid5.setCol1("销售利润/赠送");
		rebateGrid5.setCol2("------以下是销售利润/赠送(你的用户购买后，你获得的利润和赠送)------");
		rebateGrid5.setSort(5);
		rebateGridRepository.save(rebateGrid5);

		RebateGrid rebateGrid6 = new RebateGrid();
		rebateGrid6.setProductId(product.getId());
		rebateGrid6.setCol1("积分赠送");
		rebateGrid6.setCol2("粉丝" + "0.00积分" + "   会员" + product.getChiefBaseVipPoint().setScale(2, RoundingMode.HALF_UP)
				+ "积分" + "   代理" + product.getChiefHighVipPoint().setScale(2, RoundingMode.HALF_UP) + "积分");
		rebateGrid6.setSort(6);
		rebateGridRepository.save(rebateGrid6);

		RebateGrid rebateGrid7 = new RebateGrid();
		rebateGrid7.setProductId(product.getId());
		rebateGrid7.setCol1("销售利润");
		rebateGrid7.setCol2("粉丝" + "0.00贝币" + "   会员" + product.getChiefBaseVipJinBi().setScale(2, RoundingMode.HALF_UP)
				+ "贝币" + "   代理" + product.getChiefHighVipJinBi().setScale(2, RoundingMode.HALF_UP) + "贝币");
		rebateGrid7.setSort(7);
		rebateGridRepository.save(rebateGrid7);

		RebateGrid rebateGrid8 = new RebateGrid();
		rebateGrid8.setProductId(product.getId());
		rebateGrid8.setCol1("额外赠送");
		rebateGrid8.setCol2("代理可额外获得" + product.getHighVipBuyGiveOtherJb().setScale(2, RoundingMode.HALF_UP) + "~"
				+ product.getFansBuyGiveOtherJb().setScale(2, RoundingMode.HALF_UP) + "贝币(随机)");
		rebateGrid8.setSort(8);
		rebateGridRepository.save(rebateGrid8);
	}

	@Override
	public Page<RebateGrid> findRebateGridPage(Long productId, Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		return rebateGridRepository.findRebateGridPage(productId, pageNo, pageSize, searchParams, sortParam, sortType);
	}

	@Override
	public RebateGrid findById(Long id) {
		return (RebateGrid) rebateGridRepository.getById(id);
	}

	@Override
	public RebateGrid executeUpdate(Long id, String col1, String col2, Integer sort) {
		RebateGrid rebateGrid = (RebateGrid) rebateGridRepository.getById(id);
		if (null == rebateGrid) {
			return null;
		}
		if (StrUtils.isNotNULL(col1)) {
			rebateGrid.setCol1(col1);
		}
		if (StrUtils.isNotNULL(col2)) {
			rebateGrid.setCol2(col2);
		}
		if (null != sort) {
			rebateGrid.setSort(sort);
		}
		rebateGridRepository.update(rebateGrid);
		return rebateGrid;
	}

}
